<template>
  <div>
    <FormPanel ref="from" :list="formList" :formType="type" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />

    <FilterTable ref="table" :height="'auto'" :columns="columns" :dataSource="tableList" :onColumnsChange="columns => (this.columns = columns)" :isSelectColumn="false"></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button size="small" type="primary" v-show="type != 'show'" :click="dataHandle.bind(this, 1)">提 交</multiuse-button>
      <multiuse-button size="small" type="primary" v-show="type != 'show'" :click="dataHandle.bind(this, 2)">保 存</multiuse-button>
      <el-button @click="closeDrawer(1)">返 回</el-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { copyUpdateData } from '@/utils/index';
import { queryDailyInfo, queryDailyByDate, saveData, submitData } from '@/api/dealerSale/DealerDaily/Dealermaintain';

export default {
  name: 'addModel',
  props: ['type', 'rowData'], // 上个页面带入的值
  data() {
    this.saveData = null;
    this.BaseTable = null;
    return {
      formList: this.createdFrom(),
      columns: this.createdTable(),
      tableList: [],
      fromData: {},
      dictArr: [],
      deptId: '',
      officeId: ''
    };
  },
  created() {
    this.init();
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  mixins: [dictionary],
  methods: {
    // 翻译字典
    transDictText(val, dirtArr) {
      console.log(dirtArr, '---翻译---');
      let res = '';
      if (!val) return res;
      if (Array.isArray(dirtArr)) {
        const target = dirtArr.find(x => x.value == val);
        res = target ? target.text || '' : val;
      }
      return res;
    },
    async init() {
      let res = {};
      if (this.type == 'add') {
        res = await queryDailyInfo({ startType: 10031001 });
      } else {
        // moment(this.rowData.applyDate).format('YYYY-MM-DD HH:mm:ss')
        res = await queryDailyByDate({ applyDate: this.rowData.applyDate });
        if (this.type == 'show') {
          this.formList.find(x => x.fieldName == 'regionalManagerId').rules[0].required = false;
          this.formList.find(x => x.fieldName == 'applyDate').rules[0].required = false;
          this.columns.map(x => {
            if (x.editable) {
              x.editable = false;
            }
          });
        }
      }
      if (res.resultCode == 200) {
        this.dictArr = res.data.regionalManagers.map(x => ({ text: x.userName, value: x.userId }));
        this.deptId = res.data.deptId;
        this.officeId = res.data.officeId;
        this.formList.map(x => {
          if (x.fieldName == 'regionalManagerId') {
            x.itemList = this.dictArr;
            x.initialValue = res.data['regionalManagerId'];
          } else if (x.fieldName == 'applyDate') {
            x.initialValue = res.data.ttVsDailyDTOs ? res.data.ttVsDailyDTOs[0].applyDate : moment().format('YYYY-MM-DD');
          } else {
            x.initialValue = res.data[x.fieldName];
          }
        });

        this.tableList = res.data.modelCacheVOList ? JSON.parse(JSON.stringify(res.data.modelCacheVOList)) : res.data.ttVsDailyDTOs || [];
      }
    },
    async dataHandle(type) {
      let table = this.BaseTable.GET_REQUIRED_ERROR();
      this.$refs.from.SUBMIT_FORM();
      // 处理保存数据；
      this.saveData.deptId = this.deptId;
      this.saveData.officeId = this.officeId;
      this.saveData.regionalManager = this.transDictText(this.saveData.regionalManagerId, this.dictArr);
      this.saveData.ttVsDailyDTOs = this.tableList;

      this.fromData = copyUpdateData(this.fromData, this.saveData);
      if (!table && this.saveData) {
        let res = {};
        if (1 == type) {
          // 提交加状态；
          this.fromData.dailyStatus = 60621002;
          res = await submitData(this.fromData);
        }
        if (2 == type) {
          this.fromData.dailyStatus = 60621001;
          res = await saveData(this.fromData);
        }
        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });
          this.$emit('close', false, 'refresh');
        }
      }
    },

    closeDrawer() {
      this.$emit('close');
    },
    changeHandle(val) {
      this.saveData = val;
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '事业部',
          fieldName: 'deptName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '办事处',
          fieldName: 'officeName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '区域经理',
          fieldName: 'regionalManagerId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'DATE',
          label: '上报日期',
          fieldName: 'applyDate',
          dateType: 'exactdate',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '车型',
          dataIndex: 'modelName'
        },
        {
          title: '展厅访问',
          dataIndex: 'exhibitionHallVisit',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        },
        {
          title: '来电/网络',
          dataIndex: 'callOrNetwork',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        },
        {
          title: '外拓',
          dataIndex: 'promoting',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        },
        {
          title: '留档',
          dataIndex: 'maintained',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        },
        {
          title: '试驾',
          dataIndex: 'testDrive',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        },
        {
          title: '新增订单',
          dataIndex: 'newOrder',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        },
        {
          title: '累计未交',
          dataIndex: 'accumulatedUnpaid',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        },
        {
          title: '开票',
          dataIndex: 'billCount',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true,
          min: 0,
          max: 999999999
        }
      ];
    }
  }
};
</script>
